from django.db import models

# Create your models here.
from django.db import models
from django.contrib.auth import get_user_model

class Inspection_scheme(models.Model):
    scheme_number = models.CharField(max_length=30,primary_key=True)
    scheme_name = models.CharField(max_length=128)
    inspect_cycle = models.CharField(max_length=30)
    effective_date = models.DateField()
    expiration_date = models.DateField()
    remark = models.TextField()

    class Mate:
        verbose_name = "巡检方案"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.scheme_number

class Maintenance_scheme(models.Model):
    scheme_number = models.CharField(max_length=30,primary_key=True)
    scheme_name = models.CharField(max_length=128)
    maintenance_cycle = models.CharField(max_length=30)
    effective_date = models.DateField()
    expiration_date = models.DateField()
    remark = models.TextField()

    class Mate:
        verbose_name = "保养方案"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.scheme_number

class Department(models.Model):
    department_id = models.CharField(max_length=30,primary_key=True)
    department_name = models.CharField(max_length=128)

    class Mate:
        verbose_name = "部门表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.department_id

class Person(models.Model):
    person_id = models.CharField(max_length=30,primary_key=True)
    person_name = models.CharField(max_length=128)

    class Mate:
        verbose_name = "人员表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.person_id

class Equipment_type(models.Model):
    equipment_type_id = models.CharField(max_length=30,primary_key=True)
    equipment_type_name = models.CharField(max_length=128)
    inspection_scheme = models.ForeignKey(Inspection_scheme,on_delete=models.CASCADE,related_name='equipment_type_inspection_scheme_number')
    maintenance_scheme = models.ForeignKey(Maintenance_scheme,on_delete=models.CASCADE,related_name='equipment_type_maintenance_scheme_number')
    remark = models.TextField()

    class Mate:
        verbose_name = "设备类型"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.equipment_type_id

class Equipment_info(models.Model):
    equipment_number = models.CharField(max_length=30,primary_key=True)
    equipment_type = models.ForeignKey(Equipment_type,on_delete=models.CASCADE,related_name='equipment_type_id_for_equipment')
    equipment_name = models.CharField(max_length=128)
    manufacturer = models.CharField(max_length=128)
    equipment_models = models.CharField(max_length=128)
    equipment_specification = models.CharField(max_length=128)
    acceptance_date = models.DateField()
    production_date = models.DateField()
    warranty_date = models.DateField()
    Scrap_date =models.DateField()
    user_department = models.ForeignKey(Department,on_delete=models.CASCADE,related_name='belongs_department_id_for_equipment')
    responsible = models.ForeignKey(Person,on_delete=models.CASCADE,related_name='responsible_person_id_for_equipment')
    administrator = models.ForeignKey(Person,on_delete=models.CASCADE,related_name='admin_person_id_for_equipment')
    install_location = models.CharField(max_length=256)
    security_level = models.CharField(max_length=10)
    equipment_status = models.CharField(max_length=10)

    class Mate:
        verbose_name = "设备信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.equipment_number

class Technique_document(models.Model):
    document_number = models.CharField(max_length=30,primary_key=True)
    document_name = models.CharField(max_length=128)
    document_type = models.CharField(max_length=30)
    document_maker = models.CharField(max_length=128)
    e_number = models.ForeignKey(Equipment_info,on_delete=models.CASCADE,related_name='equipment_number_for_techdoc')
    document_desc = models.TextField()
    attachments = models.FileField()

    class Mate:
        verbose_name = "技术文档"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.document_number

class Part_info(models.Model):
    part_model = models.CharField(max_length=30,primary_key=True)
    part_name = models.CharField(max_length=128)
    current_stock = models.CharField(max_length=30)
    minimum_stock = models.CharField(max_length=30)
    equipment_number = models.ForeignKey(Equipment_info, on_delete=models.CASCADE,related_name='equipment_number_for_part')

    class Mate:
        verbose_name = "配件信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.part_model

class Purchase_apply(models.Model):
    apply_number = models.CharField(max_length=30,primary_key=True)
    apply_date = models.DateField()
    equipment_name = models.CharField(max_length=128)
    apply_user = models.ForeignKey(Person,on_delete=models.CASCADE,related_name='purchase_person_id')
    price = models.CharField(max_length=30)
    count = models.CharField(max_length=30)
    total_cost = models.CharField(max_length=30)
    apply_status = models.CharField(max_length=30)
    remark = models.CharField(max_length=30)

    class Mate:
        verbose_name = "购买申请"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.apply_number


class Purchase_record(models.Model):
    record_number = models.CharField(max_length=30,primary_key=True)
    purchase_equipment = models.CharField(max_length=128)
    purchase_date = models.DateField()
    remark = models.TextField()

    class Mate:
        verbose_name = "购买记录"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.record_number

class Scrapped_apply(models.Model):
    apply_number = models.CharField(max_length=30, primary_key=True)
    apply_date = models.DateField()
    equipment_name = models.CharField(max_length=128)
    apply_user = models.ForeignKey(Person, on_delete=models.CASCADE,related_name='scrapp_person_id')
    residual_value = models.CharField(max_length=30)
    count = models.CharField(max_length=30)
    total_value = models.CharField(max_length=30)
    apply_status = models.CharField(max_length=30)
    remark = models.TextField()

    class Mate:
        verbose_name = "报废申请"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.apply_number

class Scrapped_record(models.Model):
    record_number = models.CharField(max_length=30,primary_key=True)
    e_number = models.ForeignKey(Equipment_info,on_delete=models.CASCADE,related_name='scrapp_equipment_number')
    scrapped_date = models.DateField()
    remark = models.TextField()

    class Mate:
        verbose_name = "报废记录"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.record_number

class Change_apply(models.Model):
    apply_number = models.CharField(max_length=30, primary_key=True)
    apply_date = models.DateField()
    equipment_name = models.CharField(max_length=128)
    apply_department = models.ForeignKey(Department,on_delete=models.CASCADE,related_name='change_department_id')
    apply_reason = models.TextField()
    user_department = models.TextField()
    apply_status = models.CharField(max_length=30)
    remark = models.TextField()

    class Mate:
        verbose_name = "异动申请"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.apply_number

class Change_record(models.Model):
    record_number = models.CharField(max_length=30, primary_key=True)
    e_number = models.ForeignKey(Equipment_info, on_delete=models.CASCADE,related_name='change_equipment_number')
    change_date = models.DateField()
    remark = models.TextField()

    class Mate:
        verbose_name = "移动记录"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.record_number

class Repair_apply(models.Model):
    apply_number = models.CharField(max_length=30, primary_key=True)
    apply_date = models.DateField()
    equipment_name = models.CharField(max_length=128)

    class Mate:
        verbose_name = "维修申请"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.apply_number

class Repair_record(models.Model):
    record_number = models.CharField(max_length=30,primary_key=True)
    equipment_number = models.ForeignKey(Equipment_info, on_delete=models.CASCADE,related_name='repair_equipment_number')
    repair_date = models.DateField()
    repairman = models.ForeignKey(Person,on_delete=models.CASCADE,related_name='repairman_id')
    part_number = models.ForeignKey(Part_info,on_delete=models.CASCADE,related_name='part_model_for_used_in_repair')
    part_count = models.CharField(max_length=30)
    remark = models.TextField()

    class Mate:
        verbose_name = "维修记录"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.record_number

class Patrol_record(models.Model):
    record_number = models.CharField(max_length=30, primary_key=True)
    equipment_number = models.ForeignKey(Equipment_info, on_delete=models.CASCADE,related_name='patrol_equipment_number')
    patrol_date = models.DateField()
    inspection_scheme = models.ForeignKey(Inspection_scheme,on_delete=models.CASCADE,related_name='belongs_inspect_scheme_number')
    patrol_result = models.CharField(max_length=30)
    patrolman = models.ForeignKey(Person,on_delete=models.CASCADE,related_name='patrloman_id')

    class Mate:
        verbose_name = "巡检记录"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.record_number

class Maintain_record(models.Model):
    record_number = models.CharField(max_length=30, primary_key=True)
    equipment_number = models.ForeignKey(Equipment_info, on_delete=models.CASCADE,related_name='equipment_number_for_maintain')
    maintain_date = models.DateField()
    maintain_scheme = models.ForeignKey(Maintenance_scheme,on_delete=models.CASCADE,related_name='maintain_scheme_number')
    maintainman = models.ForeignKey(Person,on_delete=models.CASCADE,related_name='maintainman_id')
    part_number = models.ForeignKey(Part_info,on_delete=models.CASCADE,related_name='part_model_for_maintain')
    part_count = models.CharField(max_length=30)
    remark = models.TextField()

    class Mate:
        verbose_name = "保养记录"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.record_number